package com.example.service.impl.dm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.entity.User;
import com.example.mapper.dm.UserMapper;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户服务达梦数据库实现
 */
@Slf4j
@Service
@Profile("dm")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean saveUser(User user) {
        log.info("达梦数据库: 保存用户信息");
        if (user.getCreateTime() == null) {
            user.setCreateTime(new Date());
        }
        user.setUpdateTime(new Date());
        return userMapper.insert(user) > 0;
    }

    @Override
    public User getUserById(Long id) {
        log.info("达梦数据库: 根据ID获取用户信息");
        return userMapper.selectById(id);
    }

    @Override
    public List<User> listUsers() {
        log.info("达梦数据库: 获取所有用户");
        return userMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<User> listUsersOrderByAgeDesc() {
        log.info("达梦数据库: 获取所有用户并按年龄降序排序");
        return userMapper.selectAllOrderByAgeDesc();
    }

    @Override
    public boolean updateUser(User user) {
        log.info("达梦数据库: 更新用户信息");
        user.setUpdateTime(new Date());
        return userMapper.updateById(user) > 0;
    }

    @Override
    public boolean deleteUser(Long id) {
        log.info("达梦数据库: 删除用户");
        return userMapper.deleteById(id) > 0;
    }
}